package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Bankcard;
import cn.wolfcode.trip.base.mapper.BankcardMapper;
import cn.wolfcode.trip.base.service.IBankcardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BankcardServiceImpl implements IBankcardService {
    @Autowired
    private BankcardMapper bankcardMapper;

    @Override
    public void saveOrUpdate(Bankcard entity) {
        if (entity == null) {
            bankcardMapper.insert(entity);
        } else {
            bankcardMapper.updateByPrimaryKey(entity);
        }
    }

    @Override
    public void delete(Long id) {
        bankcardMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Bankcard> selectAllByUserId(Long id) {
        return bankcardMapper.selectAllByUser(id);
    }

    /**
     * 修改银行卡金额
     *
     * @param id
     * @param balance
     */
    @Override
    public void updateMoney(Long id, Double balance) {
        bankcardMapper.updateMoney(id, balance);
    }

    /**
     * 绑定银行卡
     *
     * @param bankcard
     */
    @Override
    public void insetBankcard(Bankcard bankcard) {
        Date data = new Date();
        //判断卡号不能为空
        if (bankcard.getCard() == null) {
            throw new RuntimeException("卡号不能为空");
        }
        //哪家的银行卡
        if (bankcard.getCardnameId() == 0) {
            bankcard.setCardname("中国银行");
            bankcard.setHeadImgUrl("/css/银行卡_files/BOC.png");
        } else if (bankcard.getCardnameId() == 1) {
            bankcard.setCardname("建设银行");
            bankcard.setHeadImgUrl("/css/银行卡_files/CCB.png");
        } else if (bankcard.getCardnameId() == 2) {
            bankcard.setCardname("招商银行");
            bankcard.setHeadImgUrl("/css/银行卡_files/BOC.png");
        }
        System.out.println(bankcard.getCard());
        System.out.println(bankcard.getCardname());
        //判断该卡号是否以存在
        Bankcard b = bankcardMapper.selectCard(bankcard.getCard(), bankcard.getCardname());
        if (b == null) {
            //设置添加卡的类型
            if (bankcard.getTypeinfoId() == 0) {
                bankcard.setTypeinfo("储蓄卡");
            } else if (bankcard.getTypeinfoId() == 1) {
                bankcard.setTypeinfo("信用卡");
            }
            bankcard.setCreationTime(data);
            bankcardMapper.insert(bankcard);
        } else {
            throw new RuntimeException("卡号已存在");
        }

    }

    @Override
    public Integer selectCount() {
        return bankcardMapper.selectCount();
    }

    @Override
    public Bankcard selectBankcardName(Long id) {
        return bankcardMapper.selectBankcardName(id);
    }
}
